package com.aqie.easy.design;

import java.util.Stack;

/**
 * 155 最小栈
 */
public class MinStack {
    private Stack<Integer> stack;
    // 保证上面元素一定比下面所有元素都小
    private Stack<Integer> min;
    /** initialize your data structure here. */
    public MinStack() {
        stack = new Stack<>();
        min = new Stack<>();
    }

    public void push(int x) {
        if (min.empty() || min.peek() >= x) min.push(x);
        stack.push(x);
    }

    public void pop() {
        if (stack.pop().equals(min.peek())){
            min.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return min.peek();
    }


    public static void main(String[] args) {
        MinStack stack = new MinStack();
        stack.push(5);
        stack.push(1);
        stack.push(4);
        stack.push(2);
        stack.push(3);
        stack.push(-1);
        System.out.println(stack.top());
        System.out.println(stack.getMin());
        stack.pop();
        System.out.println(stack.getMin());
    }
}
